﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class FrmSearchqinggourenyuan : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tb;
        public FrmSearchqinggourenyuan(TextBox tb)
        {
            InitializeComponent();
            this.tb = tb;
        }

        private void FrmSearchqinggourenyuan_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_請購人員訊息_查詢";
                //string strcmd = " Select * From 圖面廠區";
                SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                //cmd.SelectCommand.Parameters.AddWithValue("@廠區名稱","f");
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dtgSearch1.DataSource = dt;
                PublicClass.setRowNumber(dtgSearch1);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnSearchTypeOK_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dtgSearch1.CurrentCell.RowIndex;
                string sup請購人員Id = dtgSearch1.Rows[r].Cells[0].Value.ToString();
                tb.Text = sup請購人員Id;
                this.Close();
            }
            catch
            {

            }
        }

        private void btnSearch1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_請購人員訊息_查詢";
                SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                //if (txt廠區.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@廠區代號", txt廠區.Text);
                if (txtSearch請購人員.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@請購人員代號或名稱", txtSearch請購人員.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dtgSearch1.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void dtgSearch1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dtgSearch1);
        }

        private void txtSearch請購人員_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch請購人員.Text == "")
            {
                SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    con.Open();
                    string strcmd = "sp_請購人員訊息_查詢";
                    //string strcmd = " Select * From 圖面廠區";
                    SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                    cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                    //cmd.SelectCommand.Parameters.AddWithValue("@廠區名稱","f");
                    DataTable dt = new DataTable();
                    cmd.Fill(dt);
                    dtgSearch1.DataSource = dt;
                    PublicClass.setRowNumber(dtgSearch1);
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    con.Close();
                }
            }
        }
    }
}
